/**
 * 
 */
package com.wubo.sec.model;

import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

/** 菜单
 * @author wubo
 * @CreateDate 2010-7-30
 * @version 1.0.01
 */
@Entity
@Table(name="SEC_MENU")
public class Menu extends Resource implements Comparable<Menu>, Cloneable {
	private static final long serialVersionUID = 2744363860104053719L;

	@Column(nullable=false, length=50)
	private String name; //菜单名称
	
	private int orderIndex; //同等级顺序索引
	
	private int grade; //菜单深度
	
	@Column(length=100)
	private String icon; //菜单图标
	
	private String desn; //描述

	@Column(length=32)
	private String parentCode; //父菜单编码
	
	private boolean leaf; //是否是叶子节点
	
	private boolean locked; //是否锁定
	
	@Column(name="filterable")
	private boolean filter; //是否参与权限过滤
	
	private boolean active; //是否可用
	
	private Date createTime; //创建时间
	private String createUser; //创建用户
	private Date updateTime; //修改时间
	private String updateUser; //修改用户
	
	@Transient
	private Set<Menu> subMenus;
	
	@ManyToOne(targetEntity=Authority.class, fetch=FetchType.LAZY)
	@JoinTable(
		name="SEC_MENU_AUTH",
		joinColumns=@JoinColumn(name="MENU_REL"),
		inverseJoinColumns=@JoinColumn(name="AUTH_REL")
	)
	private Authority auth; //菜单对应权限（单向关联）
	
	public String getParentCode() {
		return parentCode;
	}

	public void setParentCode(String parentCode) {
		this.parentCode = parentCode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getOrderIndex() {
		return orderIndex;
	}

	public void setOrderIndex(int orderIndex) {
		this.orderIndex = orderIndex;
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getDesn() {
		return desn;
	}

	public void setDesn(String desn) {
		this.desn = desn;
	}

	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public boolean isLocked() {
		return locked;
	}

	public void setLocked(boolean locked) {
		this.locked = locked;
	}

	public boolean isFilter() {
		return filter;
	}

	public void setFilter(boolean filter) {
		this.filter = filter;
	}

	public Authority getAuth() {
		return auth;
	}

	public void setAuth(Authority auth) {
		this.auth = auth;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}

	public Set<Menu> getSubMenus() {
		return subMenus;
	}

	public void setSubMenus(Set<Menu> subMenus) {
		this.subMenus = subMenus;
	}
	
	public int hashCode(){
		return this.code == null ? 32 : this.code.hashCode() + 32;
	}
	
	public void initSubMenus(){
		if(subMenus == null){
			subMenus = new TreeSet<Menu>();
		}
	}
	
	public int compareTo(Menu m) {
		// TODO Auto-generated method stub
		if(this.getCode().equals(m.getCode())){
			return 0;
		}
		int orderIndex = m.getOrderIndex();
		if(this.orderIndex > orderIndex){
			return 1;
		}else{
			return -1;
		}
	}
	
	public boolean equals(Object obj){
		if(obj == null) return false;
		if(! (obj instanceof Menu) ) return false;
		Menu resource = (Menu) obj;
		
		if(resource.getCode() != null && this.getCode() != null){
			return this.getCode().equals(resource.getCode());
		} else {
			return this.getUrl().equals(resource.getUrl());
		}
	}
	
	public Menu clone(){
		try{
			return (Menu) super.clone();
		}catch(CloneNotSupportedException e){
			return null;
		}
	}
}
